/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyectos.bean;

import com.proyectos.ejb.PreEjbLocal;
import com.proyectos.model.Preguntas;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Jose
 */
public class CtrPreguntas implements Serializable {

    @EJB
    private PreEjbLocal ejb;

    private String name;
    private List<Preguntas> lista;
    private Preguntas activeReg;
    private boolean lnuevo;
    private CtrEntorno oentorno;

    public CtrEntorno getOentorno() {
        return oentorno;
    }

    public void setOentorno(CtrEntorno oentorno) {
        this.oentorno = oentorno;
    }

    public Preguntas getActiveReg() {
        return activeReg;
    }

    public void setActiveReg(Preguntas activeReg) {
        this.activeReg = activeReg;
    }

    public boolean isLnuevo() {
        return lnuevo;
    }

    public void setLnuevo(boolean lnuevo) {
        this.lnuevo = lnuevo;
    }


    public void nuevo() {
        try {
            this.lnuevo = true;
            this.activeReg = new Preguntas();
            this.activeReg.setNcarrera(this.oentorno.getCarrera());
            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
            options.put("contentHeight", 200);
            options.put("contentWidth", 500);
            RequestContext.getCurrentInstance().openDialog("mantPreguntas", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void edit(BigDecimal id) {
        try {
            this.lnuevo = false;
            this.activeReg = this.ejb.get(id);

            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
            options.put("contentHeight", 200);
            options.put("contentWidth", 500);
            RequestContext.getCurrentInstance().openDialog("mantPreguntas", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void save(ActionEvent event) {
        try {

            if (this.lnuevo) {
                this.ejb.add(activeReg);
            } else {
                this.ejb.save(activeReg);
            }
            this.buscar();
            RequestContext.getCurrentInstance().closeDialog(null);
        } catch (Exception ex) {
            this.addMessage(ex.getMessage());
            //Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public List<Preguntas> getLista() {
        return lista;
    }

    public void setLista(List<Preguntas> lista) {
        this.lista = lista;
    }

    public void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR: ", summary);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void buscar() {
        TreeMap vparametros = new TreeMap();
        vparametros.put("ncarrera", this.oentorno.getCarrera());
        if (name != null && !name.isEmpty()) {

            vparametros.put("cnombre", "%" + name.toUpperCase() + "%");
        } else {
            vparametros.put("cnombre", "%");
        }
        lista = this.ejb.query("Preguntas.findByLike", vparametros);

    }

    public void buscar_todo() {

        this.buscar();

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void delete(ActionEvent event) {
        try {

            this.ejb.delete(activeReg.getNcodigo());
        } catch (Exception ex) {

            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
        RequestContext.getCurrentInstance().closeDialog(null);
        this.buscar();
    }

      public List<Preguntas> getVlista() {
        TreeMap vparametros = new TreeMap();
        vparametros.put("ncarrera",this.oentorno.getCarrera());
        return this.ejb.query("Preguntas.findAll", vparametros);

    }

}
